package com.exam.controller;

import com.exam.pojo.*;
import com.exam.service.*;
import com.google.gson.Gson;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 黄星
 * 成绩管理页面控制器
 */
@Controller
@RequestMapping("/ScoreManage")
public class ScoreManage {

	@Resource(name = "mv")
	private ModelAndView mv;
	@Resource(name="paging")
	Paging<Score> paging;
	@Resource(name="map")
	private Map map;
	@Resource(name="gson")
	private Gson gson;
	@Resource(name = "scoreServiceImpl")
	private ScoreService scoreService;
	@Resource(name = "subjectServiceImpl")
	private SubjectService subjectService;
	@Resource(name="majorServiceImpl")
	private MajorService majorService;
	@Resource(name="classesServiceImpl")
	private ClassesService classesService;
	@Resource(name = "typeServiceImpl")
	private TypeService typeService;
	@Resource(name = "studentServiceImpl")
	private StudentService studentService;
	@Resource(name="student")
	private Student student;

	/**
	 * 成绩管理页面的显示
	 * */
	@RequestMapping("/score-list")
	public ModelAndView ScoreList(){

		mv.addObject( "majorList",majorService.getAllMajor() );  //初始化界面；
		mv.setViewName( "admin/score-list" );
		return mv;
	}

	/**
	 * 通过专业ID 来获取所有的班级
	 * */
	@RequestMapping("/getClassesNameByMajor")
	public void getClassesNameByMajor(HttpServletRequest req,HttpServletResponse resp) throws IOException {

		resp.setContentType("text/xml;charset=UTF-8");
		resp.setCharacterEncoding("UTF-8");
		PrintWriter printWriter = resp.getWriter();

		int majorId = Integer.parseInt( req.getParameter( "majorId" ) );
		List<Classes> classes = classesService.getClassesByMajorId( majorId );
		Paging<Classes> list = new Paging<Classes>();
		list.setList( classes );
		String s = gson.toJson( list );
		printWriter.println( s );
		printWriter.close();

	}

	/**
	 * 通过专业以及班级  来获取科目
	 * */
	@RequestMapping("/getSubjectByMajorAndClasses")
	public void getSubjectByMajorAndClasses(HttpServletRequest req, HttpServletResponse resp) throws IOException {

		resp.setContentType("text/xml;charset=UTF-8");
		resp.setCharacterEncoding("UTF-8");
		PrintWriter printWriter = resp.getWriter();

		Integer majorId = Integer.parseInt( req.getParameter( "majorId" ) );
		Integer classesId = Integer.parseInt( req.getParameter( "classesId" ) );

		List<Subject> subs = subjectService.getSubjectByMajorIdAndClassesId( majorId, classesId );


		Paging<Subject> paging = new Paging<Subject>();
		paging.setList( subs );
		String s = gson.toJson(paging);
		printWriter.write(s);
		printWriter.close();
	}

	/**
	 * 分页显示成绩
	 * */
	@RequestMapping("/getScoreByPaging")
	public void getScoreByPaging(HttpServletRequest req, HttpServletResponse resp) throws IOException {

		resp.setContentType("text/xml;charset=UTF-8");
		resp.setCharacterEncoding("UTF-8");
		PrintWriter printWriter = resp.getWriter();

		Integer page = Integer.parseInt(req.getParameter("page")); //当前页
		Integer majorId = Integer.parseInt( req.getParameter( "majorId" ) );
		Integer  classesId =  Integer.parseInt( req.getParameter( "classesId" ) );
		Integer sno = Integer.parseInt( req.getParameter( "sno" ) );
		Integer subjectId = Integer.parseInt( req.getParameter( "subjectId" ) );
		Integer pageSize = 10; //页面大小
		Map map = new HashMap();
		map.put("page",page);
		map.put("pageSize",pageSize);
		map.put("majorId",majorId);
		map.put("classesId",classesId);
		map.put( "sno",sno );
		map.put( "subjectId",subjectId );
		Paging<Score> pagingList = scoreService.scorePaging(map);

		Paging<StudentAndSubject> ret = new Paging<StudentAndSubject>();
		ret.setTotalPage( pagingList.getTotalPage() );
		ret.setCurrentPage( pagingList.getCurrentPage() );
		ret.setPageSize( pagingList.getPageSize() );
		ret.setTotalCount( pagingList.getTotalCount() );
		List<StudentAndSubject> sas = new ArrayList<StudentAndSubject>(  );
		for(int i = 0;i < pagingList.getList().size();i++){
			StudentAndSubject s = new StudentAndSubject();
			s.setId( pagingList.getList().get( i ).getId() );
			s.setStudent( studentService.getStudentBySno( pagingList.getList().get( i ).getSno() ) );
			s.setSubject( subjectService.getSubjectById( pagingList.getList().get( i ).getSubjectId() ) );
			s.setResult(pagingList.getList().get( i ).getResult() );
			sas.add( s );
		}
		ret.setList( sas );
		String s = gson.toJson(ret);
		printWriter.write(s);
		printWriter.close();
	}

	/**
	 * 修改成绩页面
	 */
	@RequestMapping("/updScorePage")
	public ModelAndView updScorePage(HttpServletRequest req){

		Integer id = Integer.parseInt( req.getParameter( "id" ) );
		Score score = scoreService.getScoreById( id );
		StudentAndSubject sas = new StudentAndSubject();
		sas.setId( score.getId() );
		sas.setStudent( studentService.getStudentBySno( score.getSno() ) );
		sas.setSubject( subjectService.getSubjectById( score.getSubjectId() ) );
		sas.setResult( score.getResult() );
		mv.addObject( "score",sas );
		mv.setViewName( "admin/score-update" );
		return mv;
	}

	/**
	 * 更新按钮确认点击后
	 * */
	@RequestMapping("/updScore")
	public void updScore(HttpServletRequest req, HttpServletResponse resp) throws IOException{

		resp.setContentType("text/xml;charset=UTF-8");
		resp.setCharacterEncoding("UTF-8");
		req.setCharacterEncoding( "utf-8" );
		PrintWriter out = resp.getWriter();

		Integer id = Integer.parseInt( req.getParameter( "id" ) );
		Integer result = Integer.parseInt( req.getParameter( "result" ).trim() );

		if(scoreService.updScore( id,result ) > 0){
			out.println( "修改成功" );
		}else {
			out.println( "修改失败" );
		}
		out.flush();
		out.close();
	}
}
